package com.loyal.loyalerp.humenresource.contract.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loyal.loyalerp.basedata.employeechange.mapper.GyPersonChangeMapper;
import com.loyal.loyalerp.basedata.employeechange.model.GyPersonChange;
import com.loyal.loyalerp.humenresource.contract.entity.Contract;
import com.loyal.loyalerp.humenresource.contract.entity.ReturnMsg;
import com.loyal.loyalerp.humenresource.contract.mapper.ContractMapper;
import com.loyal.loyalerp.model.GyUser;

@Service
public class ContractService {
	@Autowired
	private ContractMapper mapper;
	@Autowired
	GyPersonChangeMapper changeMapper;

	// 获取所有合同信息
	public List<Contract> getAllContract() {
		return mapper.selectAllContract();
	}

	// 添加合同
	public ReturnMsg addContract(Contract contract) {
		ReturnMsg msg = new ReturnMsg();
		int personId = selectIdByCode(contract.getPersonId());
		contract.setPersonId(personId);
		if (contract.getCurrentContract() == 0) {
			mapper.addContract(contract);
			msg.setStatus(200);
			msg.setMessage("添加成功");
		} else {
			int count = mapper.IsCurrentContract(personId);
			if (count == 0) {
				mapper.addContract(contract);
				msg.setStatus(200);
				msg.setMessage("添加成功");
			} else {
				msg.setStatus(500);
				msg.setMessage("当前还有未终止的合同！");
			}
		}

		return msg;
	}

	// 搜索用户的合同信息
	public List<Contract> searchContractByName(String username) {
		List<Contract> list = new ArrayList<Contract>();
		List<Integer> personIdList = mapper.selectPersonIdByName(username);
		for (Integer i : personIdList) {
			list.addAll(mapper.selectAllContractByPersonId(i));
		}
		return list;
	}

	public int selectIdByCode(int code) {
		return mapper.selectIdByCode(code);
	}

	// 删除合同
	public void deleteContract(int id) {
		// 根据合同id获得用户id
		int personId = mapper.getPersonIdByPrimaryKey(id);
		// 删除合同
		mapper.deleteContract(id);
		// 获得所有合同列表并且降序
		List<Contract> list = mapper.contractDesc(personId);
		// 日期最晚的合同
		if (list.size() != 0) {
			Contract contract = list.get(0);
			// 设置终止日期最晚的为当前合同
			mapper.updateCurrentContract(contract.getId());
		}
	}

	// 修改合同信息
	public ReturnMsg updateContract(Contract contract) {
		ReturnMsg msg = new ReturnMsg();
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		if (null == user) {
			msg.setStatus(600);
			msg.setMessage("用户信息过期，请重新登录");
			return msg;
		}

		// 根据合同的id查询合同修改前的状态是否为当前合同
		Contract contracted = mapper.selectContractById(contract.getId());
		int personId = mapper.getPersonIdByPrimaryKey(contract.getId());
		if (contract.getCurrentContract() == 1 && contracted.getCurrentContract() == 0) {
			int count = mapper.IsCurrentContract(personId);
			if (count > 0) {
				msg.setStatus(500);
				msg.setMessage("当前已有正在生效的合同");
				return msg;
			} else {
				mapper.updateContract(contract);
				// 判断日期有没有修改
				Date updateDate = contract.getEndTime();
				Date oldDate = contracted.getEndTime();
				if (DateUtils.isSameDay(updateDate, oldDate) == false) {
					GyPersonChange change = new GyPersonChange();
					change.setPersonid(personId);
					change.setChangetype("合同续签");
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					change.setBeforechange(format.format(oldDate));
					change.setAfterchange(format.format(updateDate));
					change.setEffectdate(new Date());
					change.setMakedate(new Date());
					change.setMakerid(user.getId());
					changeMapper.insert(change);

				}
				msg.setStatus(200);
				msg.setMessage("修改成功");
				return msg;
			}
		} else {
			mapper.updateContract(contract);
			msg.setStatus(200);
			msg.setMessage("修改成功");
			// 判断日期有没有修改
			Date updateDate = contract.getEndTime();
			Date oldDate = contracted.getEndTime();
			if (DateUtils.isSameDay(updateDate, oldDate) == false) {
				GyPersonChange change = new GyPersonChange();
				change.setPersonid(personId);
				change.setChangetype("合同续签");
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				change.setBeforechange(format.format(oldDate));
				change.setAfterchange(format.format(updateDate));
				change.setEffectdate(new Date());
				change.setMakedate(new Date());
				change.setMakerid(user.getId());
				changeMapper.insert(change);
			}

			return msg;
		}
	}
}
